package com.dongqi.detectionResult.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dongqi.detectionResult.dto.DetectionResultDTO;
import com.dongqi.detectionResult.entity.DetectionResultEntity;
import com.dongqi.detectionResult.mapper.DetectionResultMapper;
import com.dongqi.detectionResult.service.DetectionResultService;
import com.dongqi.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class DetectionResultServiceImpl implements DetectionResultService {
    @Autowired
    private DetectionResultMapper detectionResultMapper;

    @Override
    public List<DetectionResultDTO> getResultByTaskNo(Map<String, String> map) {
        QueryWrapper<DetectionResultEntity> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(map.get("detectType"))){
            String detectType = map.get("detectType");
            wrapper.eq("detect_type", detectType);
        }
        if (StringUtils.isNotEmpty(map.get("detectResult"))){
            String detectResult = map.get("detectResult");
            wrapper.eq("detect_result", detectResult);
        }
        if (StringUtils.isNotEmpty(map.get("detectTaskNo"))){
            String detectTaskNo = map.get("detectTaskNo");
            wrapper.eq("detect_task_no", detectTaskNo);

        }
        List<DetectionResultEntity> detectionResultEntities = detectionResultMapper.selectList(wrapper);
        if (detectionResultEntities.size() > 0) {
            List<DetectionResultDTO> detectionResultDTOS = new ArrayList<>();
            for (DetectionResultEntity detectionResultEntity : detectionResultEntities) {
                DetectionResultDTO detectionResultDTO = new DetectionResultDTO();
                BeanUtils.copyProperties(detectionResultEntity, detectionResultDTO);
                detectionResultDTOS.add(detectionResultDTO);
            }
            return detectionResultDTOS;
        } else {
            return null;
        }


    }

    @Override
    public DetectionResultDTO getDetailByDeviceNo(String deviceNo) {
        QueryWrapper<DetectionResultEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("device_no", deviceNo);
        DetectionResultEntity entity = detectionResultMapper.selectOne(wrapper);
        String feedback = entity.getFeedback();

        return null;
    }

}
